#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
    int minFallingPathSum(vector<vector<int>>& matrix) {
        int m = matrix.size();
        vector<vector<int>> as(m + 1, vector<int>(m + 2, INT_MAX));

        for (int j = 0; j < m + 2; j++)
        {
            as[0][j] = 0;

        }

        for (int i = 1; i < m + 1; i++)
        {
            for (int j = 1; j < m + 1; j++)
            {
                as[i][j] = min(as[i - 1][j - 1], min(as[i - 1][j], as[i - 1][j + 1])) + matrix[i - 1][j - 1];
            }
        }
        int ret = INT_MAX;
        for (int i = 1; i <= m; i++)
        {
            ret = min(ret, as[m][i]);
        }
        return ret;
    }
};